package com.example.testanimation.graphics.old;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import com.nova.scenic.projectlibs.util.debug.PerformanceRecorder;


public class MyUserSuffaceView extends SurfaceView implements SurfaceHolder.Callback, Runnable {

	private boolean isStop;

	private MyAnimation mAnimation;
	SurfaceHolder holder;

	public MyUserSuffaceView(Context context) {
		super(context);
		holder = getHolder();
		holder.addCallback(this);
	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		isStop = false;
		mAnimation = new MyAnimation(getContext());
		new Thread(this).start();
	}

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		isStop = true;
	}

	public void draw() {
		synchronized (holder) {
			Canvas canvas = holder.lockCanvas(null);
			 canvas.drawColor(Color.BLACK);
			 PerformanceRecorder pr = new PerformanceRecorder("scenic");
			 pr.start(); 
			 mAnimation.draw(canvas);
			 mAnimation.refresh(0,getHeight()/2);

			holder.unlockCanvasAndPost(canvas);
			 pr.end();

		}
	}

	@Override
	public void run() {
		try {
			while (!isStop) {
				draw();
				Thread.sleep(10);
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

}
